package gm.server.virtual.simulator2D;

import gm.server.Motor;
import gm.server.MotorStepEvent;
import gm.server.Nut;
import java.text.DecimalFormat;
import javax.swing.border.TitledBorder;

/**
 *
 * @author imruf84
 */
public class NutPanel extends javax.swing.JPanel {

    /**
     * Csavar.
     */
    private final Nut nut;

    /**
     * Creates new form MotorPanel
     *
     * @param nut csavar
     */
    public NutPanel(Nut nut) {

        initComponents();
        this.nut = nut;

        Motor m = getNut().getRod().getMotor();
        ((TitledBorder) getBorder()).setTitle(m.getName());

        updatePositionData();

        getNut().getRod().getMotor().addOnStepEvent((Motor m1) -> {
            updatePositionData();
        });
    }

    /**
     * Pozíció adatok frissítése.
     */
    public final void updatePositionData() {

        Nut n = getNut();
        Motor m = n.getRod().getMotor();

        absPosStepsValueLabel.setText(new DecimalFormat("#").format(m.getCurrentStepsAbsolute()));
        absPosRotValueLabel.setText(new DecimalFormat("#").format(m.stepsToRotation(m.getCurrentStepsAbsolute())));
        absPosMmValueLabel.setText(new DecimalFormat("#").format(n.stepsToMetric(m.getCurrentStepsAbsolute())));
    }

    /**
     * Csavar lekérdezése.
     *
     * @return csavar
     */
    public final Nut getNut() {
        return nut;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel9 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        absPosStepsValueLabel = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        absPosRotValueLabel = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        absPosMmValueLabel = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel9.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        jLabel15.setText("Abszolút pozíció:");
        jPanel9.add(jLabel15);

        add(jPanel9);

        jPanel10.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        absPosStepsValueLabel.setText("-");
        jPanel10.add(absPosStepsValueLabel);

        jLabel17.setText("lépés");
        jPanel10.add(jLabel17);

        add(jPanel10);

        jPanel11.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        absPosRotValueLabel.setText("-");
        jPanel11.add(absPosRotValueLabel);

        jLabel19.setText("fordulat");
        jPanel11.add(jLabel19);

        add(jPanel11);

        jPanel12.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        absPosMmValueLabel.setText("-");
        jPanel12.add(absPosMmValueLabel);

        jLabel21.setText("mm");
        jPanel12.add(jLabel21);

        add(jPanel12);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel absPosMmValueLabel;
    private javax.swing.JLabel absPosRotValueLabel;
    private javax.swing.JLabel absPosStepsValueLabel;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel9;
    // End of variables declaration//GEN-END:variables
}
